﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;

using Shared;

namespace WorldServer
{
    public partial class Client
    {
        #region Fields

        /// <summary>
        /// Client session key.
        /// </summary>
        public int SessionKey = -1;

        /// <summary>
        /// User id of current user.
        /// </summary>
        public int UserID = -1;

        /// <summary>
        /// Username of current user.
        /// </summary>
        public string Username = "";

        /// <summary>
        /// Password of current user.
        /// </summary>
        public string Password = "";

        /// <summary>
        /// Client socket.
        /// </summary>
        public Socket ClientSocket;

        /// <summary>
        /// Declares if the client is being handled or not.
        /// </summary>
        public bool IsBusy = false;

        /// <summary>
        /// In case of split packets, remains are put here.
        /// </summary>
        public byte[] Remains;

        #endregion

        #region Methods

        /// <summary>
        /// Destroys the current client.
        /// </summary>
        public void Destruct(string Reason)
        {
            try
            {
                if (this.ClientSocket.Connected)
                {
                    this.ClientSocket.Disconnect(false);
                    this.ClientSocket.Close();
                }
            }
            finally
            {
                this.Write(Reason);
                Program.SessionManager.Destroy(this.SessionKey);
                Program.ClientManager.DeleteClient(this);
            }
        }

        /// <summary>
        /// Formats and writes an user message to logger.
        /// </summary>
        /// <param name="Message">Message to be logged.</param>
        /// <param name="Arguments">Format arguments.</param>
        public void Write(string Message, params object[] Arguments)
        {
            Message = string.Format(Message, Arguments);

            this.Write(Message);
        }

        /// <summary>
        /// Writes an user message to logger.
        /// </summary>
        /// <param name="Message">Message to be logged.</param>
        public void Write(string Message)
        {
            string User = "[Unknown User]";

            if (this.Username != "")
            {
                User = "[" + this.Username + "]";
            }

            Program.Logger.Write(LogType.User, User + " - " + Message);
        }

        #endregion

        #region Packet Methods

        /// <summary>
        /// Sends the session key to client.
        /// </summary>
        public void SendSessionKey()
        {
            SendPacket Packet = new SendPacket();

            Packet.AddInt32(0);
            Packet.AddInt32(this.SessionKey);

            Packet.Send(this.ClientSocket);
        }

        #endregion
    }
}
